package com.cardbaobao.cardbabyclient.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.buried.Self;
import com.cardbaobao.cardbabyclient.ui.activity.base.BaseActivity;
import com.cardbaobao.cardbabyclient.controller.InitController;
import com.cardbaobao.cardbabyclient.utils.ApplicationHelper;

import com.cardbaobao.cardbabyclient.utils.LocationUtil;
import com.cardbaobao.cardbabyclient.utils.PhoneInfoUtil;
import com.cardbaobao.cardbabyclient.utils.UmengEventUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by zad on 2016/12/25 0025.
 * 启动页
 */
public class LaunchActivity extends BaseActivity {
    public static final String URL_FROM_WEB_S="active_url";

    @BindView(R.id.iv_logo)
    ImageView ivLogo;

    @BindView(R.id.iv_main)
    ImageView ivMain;

    private Handler handler = new Handler(Looper.getMainLooper()) {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            initIntent();
            if(!mController.getFirstStart()){
                //第一次开启应用
                mIntent.setClass(mContext,FirstShowActivity.class);
            }else{
                Intent intent=getIntent();
                if(Intent.ACTION_VIEW.equals(intent.getAction())){
                    //由网页或者其他应用唤起app，获取url，将其带到首页，再由首页跳转到webActivity打开网页
                    String dataString=intent.getDataString();
                    if(!TextUtils.isEmpty(dataString)){
                        //获取url
                        int index=dataString.indexOf("://");
                        if(index!=-1){
                            String url=dataString.substring(index+3).trim();
                            mIntent.putExtra(URL_FROM_WEB_S,url);
                        }
                    }
                }
                mIntent.setClass(mContext, MainActivity.class);
            }
            mIntent.putExtra(PRE_PAGE_ID_S,mCurrentPageId);
            mContext.startActivity(mIntent);
            finish();

        }
    };


    @Override
    protected void initView(Bundle savedInstanceState) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        ivLogo.setImageResource(R.mipmap.icon_launcher_splash_logo);

        mCurrentPageId= Self.Page.app_splash_screen_page;

        String channel=PhoneInfoUtil.getApkChannelName(mApplicationContext);
        switch (channel){
            case "huawei":
                //华为首发
                ivMain.setImageResource(R.mipmap.img_launcher_splash_huawei);
                ivMain.setBackgroundResource(R.color.color_F4A13F);
                ivLogo.setVisibility(View.VISIBLE);
                break;
            default:
                ivMain.setImageResource(R.mipmap.img_launcher_splash_normal);
                ivMain.setScaleType(ImageView.ScaleType.FIT_XY);
                ivLogo.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        handler.sendEmptyMessageDelayed(0, 3*1000);
        //每次启动时，获取当前城市、经纬度
        LocationUtil.startLocation(LaunchActivity.this,null);
        new InitController(mContext).updateUserInfo();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_launcher;
    }

    @Override
    protected void onResume() {
        super.onResume();
        Map<String,String> map = new HashMap<>();
        map.put("channel", PhoneInfoUtil.getApkChannelName(mApplicationContext));
        UmengEventUtils.clickEvent(mContext,
                ApplicationHelper.getApplicationContext().getString(R.string.event_0x03a),map);
        UmengEventUtils.pageEvent(mContext,
                ApplicationHelper.getApplicationContext().getString(R.string.event_0x000));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;//让返回键失效
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void finish() {
        //右进左出动画
        overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
        super.finish();
    }
}
